package com.aispeech.task.api.data.vo;

import com.aispeech.task.api.data.enums.Errors;
import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    private T data;
    private int errno;
    private String error;

    public Result() {
    }

    public Result(T data) {
        this.data = data;
    }

    public Result(int errno, String error) {
        this.errno = errno;
        this.error = error;
    }

    public static Result<Void> ok() {
        Result<Void> result = new Result<>(0, null);
        result.data = null;
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>(0, null);
        result.data = data;
        return result;
    }

    public static Result<Void> fail(int errno, String error) {
        Result<Void> result = new Result<>(errno, error);
        result.data = null;
        return result;
    }

    public static Result<Void> exception(Exception ex) {
        Result<Void> result = new Result<>(1, ex.getMessage());
        result.data = null;
        return result;
    }

    public static Result<Void> fail(Errors err) {
        Result<Void> result = new Result<>(err.getErrorCode(), err.getErrorDesc());
        result.data = null;
        return result;
    }

    public static Result<Void> throwable(Throwable throwable) {
        Result<Void> result = new Result<>(1, throwable.getMessage());
        result.data = null;
        return result;
    }
}
